package com.ywjt.platformbusiness.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.home.CommonModel;
import com.ywjt.platformbusiness.home.adapter.HomeGoodsTypeAdapter;
import com.ywjt.platformbusiness.home.fragment.HomeTypeGoodsFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeGoodsTypeActivity  extends BaseActivity {
    private List<CommonModel> mTitleList;
    private List<Fragment> fragmentList;
    private RecyclerView rcType;
    private HomeGoodsTypeAdapter typeAdapter;
    private ViewPager viewPager;
    private MainAdapter adapter;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, HomeGoodsTypeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.home_goods_type;
    }

    @Override
    protected void initView() {
        setTitleText("商品分类");
        rcType=findViewById(R.id.rcType);
        viewPager=findViewById(R.id.viewPager);
        mTitleList=new ArrayList<>();
        fragmentList=new ArrayList<>();

        mTitleList.add(new CommonModel("全部",true));
        mTitleList.add(new CommonModel("住宅家具",false));
        mTitleList.add(new CommonModel("厨房用具",false));
        mTitleList.add(new CommonModel("灯具五金",false));
        mTitleList.add(new CommonModel("床上家纺",false));
        mTitleList.add(new CommonModel("出行好物",false));
        mTitleList.add(new CommonModel("出行好物",false));
        mTitleList.add(new CommonModel("出行好物",false));

        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));
        fragmentList.add(HomeTypeGoodsFragment.newInstance("1"));

        typeAdapter=new HomeGoodsTypeAdapter(mTitleList);
        rcType.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL,false));
        rcType.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i=0;i<mTitleList.size();i++){
                    mTitleList.get(i).setSelected(false);
                }
                mTitleList.get(position).setSelected(true);
                typeAdapter.notifyDataSetChanged();
            }
        });

        adapter=new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i=0;i<mTitleList.size();i++){
                    mTitleList.get(i).setSelected(false);
                }
                mTitleList.get(position).setSelected(true);
                typeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position).getStr();
        }
    }
}
